package com.campus.counseling.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.counseling.model.common.Result;
import com.campus.counseling.model.dto.PsychologicalTestDTO;
import com.campus.counseling.model.query.PsychologicalTestQuery;
import com.campus.counseling.model.vo.PsychologicalTestVO;
import com.campus.counseling.service.PsychologicalTestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "心理测试管理")
@RestController
@RequestMapping("/api/psychological-tests")
@RequiredArgsConstructor
public class PsychologicalTestController {

    private final PsychologicalTestService psychologicalTestService;

    @GetMapping
    @Operation(summary = "获取测试列表")
    public Result<Page<PsychologicalTestVO>> list(PsychologicalTestQuery query) {
        return Result.success(psychologicalTestService.list(query));
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取测试详情")
    public Result<PsychologicalTestVO> getTest(@PathVariable Long id) {
        return Result.success(psychologicalTestService.getTest(id));
    }

    @PostMapping
    @Operation(summary = "新增测试")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> add(@RequestBody PsychologicalTestDTO dto) {
        psychologicalTestService.add(dto);
        return Result.success();
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新测试")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> update(@PathVariable Long id, @RequestBody PsychologicalTestDTO dto) {
        psychologicalTestService.update(id, dto);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除测试")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> delete(@PathVariable Long id) {
        psychologicalTestService.delete(id);
        return Result.success();
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新测试状态")
    @PreAuthorize("hasAnyRole('admin', 'superadmin')")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        psychologicalTestService.updateStatus(id, status);
        return Result.success();
    }

    @PostMapping("/{id}/start")
    @Operation(summary = "开始测试")
    public Result<Long> startTest(@PathVariable Long id) {
        return Result.success(psychologicalTestService.startTest(id));
    }

    @Operation(summary = "根据类型获取测试列表")
    @GetMapping("/type/{testType}")
    public Result<List<PsychologicalTestVO>> getTestsByType(@PathVariable String testType) {
        return Result.success(psychologicalTestService.getTestsByType(testType));
    }

    @Operation(summary = "获取所有测试类型")
    @GetMapping("/types")
    public Result<List<String>> getAllTestTypes() {
        return Result.success(psychologicalTestService.getAllTestTypes());
    }

    @Operation(summary = "更新测试类型")
    @PutMapping("/{testId}/type")
    public Result<Boolean> updateTestType(
            @Parameter(description = "测试ID") @PathVariable Long testId,
            @Parameter(description = "测试类型") @RequestParam String testType) {
        return Result.success(psychologicalTestService.updateTestType(testId, testType));
    }

    @Operation(summary = "批量更新测试类型")
    @PutMapping("/type/batch")
    public Result<Boolean> batchUpdateTestType(
            @Parameter(description = "测试ID列表") @RequestParam List<Long> testIds,
            @Parameter(description = "测试类型") @RequestParam String testType) {
        return Result.success(psychologicalTestService.batchUpdateTestType(testIds, testType));
    }

    @Operation(summary = "获取测试类型统计")
    @GetMapping("/type/statistics")
    public Result<Map<String, Integer>> getTestTypeStatistics() {
        return Result.success(psychologicalTestService.getTestTypeStatistics());
    }
} 